package self.hoy.demo.grainmall.cart.vo;

import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author hyh
 * @date 2020/8/16
 */
@Data
public class Cart implements Serializable {
    private static final long serialVersionUID = -2575500104338712521L;
    private List<CartItem> items;
    private Integer countNum;
    private Integer countType;
    private BigDecimal totalAmount;
    private BigDecimal reduce = BigDecimal.ZERO;//减免、优惠价格

    public Integer getCountType() {
        return items == null ? items.size() : 0;
    }

    public Integer getCountNum() {
        return items == null || items.size() == 0 ?
                items.stream().mapToInt(item -> item.getCount()).sum()
                : 0;
    }

    public BigDecimal getTotalAmount() {
        return items == null || items.size() == 0 ?
                BigDecimal.ZERO :
                items.stream().map(item -> item.getTotalPrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}
